<?php

namespace app\massage\model;

class User extends \app\BaseModel
{
    protected $name = "massage_service_user_list";
    public function getPhoneAttr($value, $data)
    {
        if(!empty($value) && isset($data["uniacid"]) && numberEncryption($data["uniacid"]) == 1) {
            return substr_replace($value, "****", 2, 4);
        }
        return $value;
    }
    public function getBalanceAttr($value, $data)
    {
        if(isset($value)) {
            return round($value, 2);
        }
    }
    public function getCashAttr($value, $data)
    {
        if(isset($value)) {
            return round($value, 2);
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $data["status"] = 1;
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page, $mapor = [], $field = "*", $top = "id desc")
    {
        $data = $this->where($dis)->where(function ($query) use ($mapor) {
            $query->whereOr($mapor);
        })->field($field)->order($top)->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis, $field = "*")
    {
        $data = $this->where($dis)->field($field)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function orderQr($input, $uniacid)
    {
        $data = longbingCreateWxCode($uniacid, $input, $input["page"]);
        $data = transImagesOne($data, ["qr_path"], $uniacid);
        $qr = $data["qr_path"];
        return $qr;
    }
    public function authCheckData($data)
    {
        $auth_server = new \app\massage\server\RoleAuth();
        $coach_model = new Coach();
        $salesman_model = new Salesman();
        $distri_model = new DistributionList();
        $channel_model = new ChannelList();
        $admin_model = new Admin();
        $role_model = new \app\mobilenode\model\RoleAdmin();
        $member_level = new \app\member\model\Level();
        $broker_model = new \app\coachbroker\model\CoachBroker();
        $admin_user = new \app\adminuser\model\AdminUser();
        $auth_server->addObserver($coach_model);
        $auth_server->addObserver($salesman_model);
        $auth_server->addObserver($distri_model);
        $auth_server->addObserver($channel_model);
        $auth_server->addObserver($admin_model);
        $auth_server->addObserver($role_model);
        $auth_server->addObserver($member_level);
        $auth_server->addObserver($broker_model);
        $auth_server->addObserver($admin_user);
        $data = $auth_server->notify($data);
        return $data;
    }
    public function checkCoachCash($coach)
    {
        $order_model = new Order();
        $wallet_model = new Wallet();
        $comm_model = new Commission();
        $coach_model = new Coach();
        $service = $order_model->where(["pay_type" => 7, "coach_id" => $coach["id"]])->sum("coach_cash");
        $balance_cash = $comm_model->where(["top_id" => $coach["id"], "type" => 7, "status" => 2])->sum("cash");
        $wallet_price = $wallet_model->where(["coach_id" => $coach["id"], "type" => 1])->where("status", "in", [1, 2])->sum("total_price");
        $cash = round($service + $balance_cash - $coach["service_price"] - $wallet_price, 2);
        $coach_model->dataUpdate(["id" => $coach["id"]], ["check_cash" => $cash]);
        return true;
    }
    public function checkAdminCash($admin)
    {
        $comm_model = new Commission();
        $admin_model = new Admin();
        $wallet_model = new Wallet();
        $admin_cash = $comm_model->where(["top_id" => $admin["id"], "status" => 2])->where("type", "in", [2, 5, 6])->sum("cash");
        $wallet_price = $wallet_model->where(["admin_id" => $admin["id"], "type" => 3])->where("status", "in", [1, 2])->sum("total_price");
        $cash = round($admin_cash - $admin["cash"] - $wallet_price, 2);
        $admin_model->dataUpdate(["id" => $admin["id"]], ["check_cash" => $cash]);
        return true;
    }
    public function getPartnerId($name, $all_config)
    {
        $where[] = ["id", "=", $name];
        $where[] = ["nickName", "like", "%" . $name . "%"];
        $id = $this->whereOr($where)->column("id");
        if($all_config == 1) {
            $distr_model = new DistributionList();
            $id = $distr_model->where(["status" => 2])->where("user_id", "in", $id)->column("user_id");
        }
        return $id;
    }
    public function orderOneUser($uniacid)
    {
        $where[] = ["a.uniacid", "=", $uniacid];
        $where[] = ["a.status", "=", 1];
        $where[] = ["b.pay_type", ">", 1];
        $data = $this->alias("a")->join("massage_service_order_list b", "a.id = b.user_id")->where($where)->field("COUNT(distinct(b.id)) as order_count,a.id")->having("order_count = 1")->group("a.id")->count();
        return $data;
    }
    public function orderTwoUser($uniacid)
    {
        $where[] = ["a.uniacid", "=", $uniacid];
        $where[] = ["a.status", "=", 1];
        $where[] = ["b.pay_type", ">", 1];
        $data = $this->alias("a")->join("massage_service_order_list b", "a.id = b.user_id")->where($where)->field("COUNT(distinct(b.id)) as order_count,a.id")->having("order_count > 1")->group("a.id")->count();
        return $data;
    }
    public function lossUser($uniacid)
    {
        $time = strtotime("-5 month", time());
        $time = strtotime(date("Y-m-d", $time));
        $where[] = ["a.uniacid", "=", $uniacid];
        $where[] = ["a.status", "=", 1];
        $where[] = ["b.pay_type", ">", 1];
        $data = $this->alias("a")->join("massage_service_order_list b", "a.id = b.user_id")->where($where)->field("COUNT(distinct(b.id)) as order_count,max(b.create_time) as create_time,a.id")->having("order_count >= 1")->having("create_time < " . $time)->group("a.id")->count();
        return $data;
    }
    public function orderNoUser($uniacid)
    {
        $count = $this->where(["uniacid" => $uniacid, "status" => 1])->count();
        $model = new Order();
        $have = $model->where(["uniacid" => $uniacid])->where("pay_type", ">", 1)->group("user_id")->count();
        return $count - $have;
    }
    public function userUseCash($user_id)
    {
        $order_model = new Order();
        $refund_model = new RefundOrder();
        $dis[] = ["user_id", "=", $user_id];
        $dis[] = ["pay_time", ">", 0];
        $pay_price = $order_model->where($dis)->sum("pay_price");
        return round($pay_price, 2);
    }
    public function getUserPid($order)
    {
        $user_model = new User();
        $reseller_model = new DistributionList();
        $admin_model = new Admin();
        if(getPromotionRoleAuth(1, $order["uniacid"]) == 0) {
            return $order;
        }
        $auth = $reseller_model->getPayResellerAuth($order["uniacid"]);
        $dis = ["id" => $order["user_id"]];
        $top_id = $user_model->where($dis)->value("pid");
        $top = $user_model->dataInfo(["id" => $top_id, "status" => 1]);
        if(empty($top)) {
            return $order;
        }
        $reseller = $reseller_model->dataInfo(["user_id" => $top["id"], "status" => 2]);
        if(getFxStatus($order["uniacid"]) == 1 && empty($reseller)) {
            return $order;
        }
        if(!empty($reseller["admin_id"]) && getConfigSetting($order["uniacid"], "distribution_range_type") == 1) {
            $admin = $admin_model->where(["id" => $reseller["admin_id"], "status" => 1])->field("city_type,city_id")->find();
            if(!empty($admin)) {
                $address = !empty($order["address_info"]["area"]) ? $order["address_info"] : getCityByLat($order["address_info"]["lng"], $order["address_info"]["lat"], $order["uniacid"]);
                if($admin["city_type"] == 1) {
                    $city_filed = "city";
                } elseif($admin["city_type"] == 2) {
                    $city_filed = "area";
                } else {
                    $city_filed = "province";
                }
                $city = $address[$city_filed];
                if(!empty($city)) {
                    $city_model = new City();
                    $city_count = $city_model->where(["id" => $admin["city_id"], "status" => 1, "title" => $city])->count();
                    if($city_count == 0) {
                        return $order;
                    }
                }
            }
        }
        $order["user_top_id"] = $top_id;
        $order["user_reseller_id"] = !empty($reseller) ? $reseller["id"] : 0;
        $order["user_reseller_balance"] = !empty($reseller) ? $reseller["balance"] : -1;
        $reseller_config = getConfigSettingArr($order["uniacid"], ["reseller_coach_balance", "reseller_admin_balance"]);
        $order = array_merge($order, $reseller_config);
        $order["reseller_company_balance"] = 100 - $order["reseller_coach_balance"] - $order["reseller_admin_balance"];
        $order["level_reseller_coach_balance"] = $order["reseller_coach_balance"];
        $order["level_reseller_admin_balance"] = $order["reseller_admin_balance"];
        $order["level_reseller_company_balance"] = $order["reseller_company_balance"];
        if(!empty($reseller["pid"])) {
            $level_reseller = $reseller_model->dataInfo(["id" => $reseller["pid"], "status" => 2]);
            if($auth) {
                if(!empty($level_reseller) && $level_reseller["reseller_level"] != 1) {
                    return $order;
                }
                if(!empty($reseller) && $reseller["reseller_level"] == 1) {
                    return $order;
                }
            }
            if(!empty($level_reseller)) {
                $order["level_top_id"] = $level_reseller["user_id"];
                $order["level_reseller_id"] = $level_reseller["id"];
                $order["level_reseller_balance"] = $level_reseller["level_balance"];
            }
        }
        return $order;
    }
    public function getRegisterLocation($input, $insert, $uniacid)
    {
        if(!empty($input["lng"]) && !empty($input["lat"])) {
            $location = getCityByLat($input["lng"], $input["lat"], $uniacid);
            $insert["province"] = $location["province"];
            $insert["city"] = $location["city"];
            $insert["area"] = $location["area"];
        } else {
            $data = getAddressByIp($uniacid);
            $data = @json_decode($data, true);
            $insert["province"] = !empty($data["result"]["ad_info"]["province"]) ? $data["result"]["ad_info"]["province"] : "";
            $insert["city"] = !empty($data["result"]["ad_info"]["city"]) ? $data["result"]["ad_info"]["city"] : "";
            $insert["area"] = !empty($data["result"]["ad_info"]["district"]) ? $data["result"]["ad_info"]["district"] : "";
        }
        return $insert;
    }
    public function userSelect($dis, $field = "*")
    {
        $data = $this->where($dis)->field($field)->order("id desc")->select()->toArray();
        return $data;
    }
}

?>